package com.itheima.app.controller;


import com.easemob.im.server.api.user.UserApi;
import com.itheima.app.exception.BusinessException;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.vo.ErrorResult;
import com.itheima.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users/phone")
@Slf4j
public class UsersController {
    @Autowired
    UserManager userManager;

    @Autowired
    UserService userService;

    /**
     * 修改手机号1.发送验证码
     * @return
     */
    @PostMapping("/sendVerificationCode")
    public void senVerificationCode(){
        //获取手机号,调用manager完成发送验证码操作
        //String phone = map.get("phone");
        userManager.sendCode(UserHolder.get().getMobile());
    }

    /**
     * 修改手机号2.校验验证码
     * @param map
     * @return
     */
    @PostMapping("/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        return userManager.checkVerificationCode(map.get("verificationCode").toString());
    }

    /**
     * 修改手机号3.保存
     * @return
     */
    @PostMapping("")
    public ResponseEntity updatePhone(@RequestBody String phone){
        Long id = UserHolder.get().getId();
        User user = userService.saveById(id);
        user.setMobile(phone);
        //更新数据
        userService.update(user);
        return ResponseEntity.ok(null);
    }



}
